<?php

class User {

	# Database reference
	private $digitalUniversity = NULL;	

	# User variables
	private $details = NULL;
	private $session = NULL;

	/*
	 * Constructor: Store SwoovaCore instance
	*/
	public function __construct(DigitalUniversity $digitalUniversity) {
	
		# Store SwoovaCore
		$this->digitalUniversity =& $digitalUniversity;
		
		# Query user
		$this->loadUser();
								
	}
	
	/*
	 * Destructor: Clean memory parts
	*/
	public function __destruct() {
		
		unset($this->digitalUniversity);
		
	}
	
	public function getUserDetails() {
		
		return $this->details;		
		# return array('details' => $this->details, 'session' => $this->sessionVariables());
		
	}
	
	/*
	 * loadUser: Fetch query session information and query user details from database
	 *
	 * @return array (
	 		'details' - An array containing the user details
			'session' - An array containing the session debug information
		)
	*/
	private function loadUser() {
		
		# Define variable
		$this->details = array();
		
		# Cookie is set
		if(isset($_COOKIE['DigitialUniversity-User'])) {
								
			# Get cookie details
			$theCookie = explode('||', $this->digitalUniversity->decryptData($_COOKIE['DigitialUniversity-User']));
			
			# Query user row
			$this->details = $this->digitalUniversity->db->getRow("SELECT u.* FROM users u
			WHERE u.loginKey = '{$theCookie[1]}' AND u.userID = '{$theCookie[0]}' LIMIT 1");	
				
			if (empty($_SESSION['timeOnline'])) {
				$_SESSION['timeOnline'] = time();
			}
							
			if (time() - $_SESSION['timeOnline'] > 60 * 15) {
				$_SESSION['timeOnline'] = time();
			}
			
			$totalTime = time() - $_SESSION['timeOnline'];
			$time = time();
						
			$this->digitalUniversity->db->query('UPDATE '.PREFIX.'users  SET 
			totalActive = (totalActive + '.$totalTime.'),		
			lastActive = "'.$time.'" 
			WHERE `uid` = "'.$theCookie[0].'"');			
			
			$this->details['totalActive'] += $totalTime;
																											
			$totalTime += time() - $_SESSION['timeOnline'];						
			$_SESSION['timeOnline'] = time();
															
			$timeDays = floor($this->details['totalActive'] / 86400).' days ';			
			$timeHours = floor(($this->details['totalActive'] % 86400) / 3600).' hours ';	
			$timeMins = floor(($this->details['totalActive'] % 3600) / 60).' minutes ';					
			$timeSecs = floor($this->details['totalActive'] % 60).' seconds ';
									
			$this->details['user_time'] = 'Online for: '.($timeDays > 0 ? $timeDays : '').($timeHours > 0 ? $timeHours : '').$timeMins.$timeSecs;
			
			unset($newActID, $newIP, $newActCode, $totalTime, $time, $timeDays, $timeHours, $timeMins, $timeSecs);
																								
		}
									
	}
	
	/*
	 * userVariables: Get the current session information including
	 * 	userIP, User Agent and Session ID
	*/
	private function sessionVariables() {
		
		$session = array();		
		$session['ip'] = $this->getIPAddress();
		$session['ssid'] = session_id();
		$session['userAgent'] = $_SERVER['HTTP_USER_AGENT'];	
		
		return $session;
																										
	}
		
	/* 
	 * getIPAddress: Return the current IP
	*/
	private function getIPAddress() {
		
		if(!empty($_SERVER['HTTP_CLIENT_IP'])) {		
			$ip = $_SERVER['HTTP_CLIENT_IP'];			
		} elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {		
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];			
		} else {		
			$ip = $_SERVER['REMOTE_ADDR'];			
		}
			
		return $ip;
	
	}
	
}

?>